/*
 * Copyright 2013 Simon Willeke
 * contact: hamstercount@hotmail.com
 */

/*
    This file is part of Blockinger.

    Blockinger is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blockinger is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Blockinger.  If not, see <http://www.gnu.org/licenses/>.

    Diese Datei ist Teil von Blockinger.

    Blockinger ist Freie Software: Sie k�nnen es unter den Bedingungen
    der GNU General Public License, wie von der Free Software Foundation,
    Version 3 der Lizenz oder (nach Ihrer Option) jeder sp�teren
    ver�ffentlichten Version, weiterverbreiten und/oder modifizieren.

    Blockinger wird in der Hoffnung, dass es n�tzlich sein wird, aber
    OHNE JEDE GEW�HELEISTUNG, bereitgestellt; sogar ohne die implizite
    Gew�hrleistung der MARKTF�HIGKEIT oder EIGNUNG F�R EINEN BESTIMMTEN ZWECK.
    Siehe die GNU General Public License f�r weitere Details.

    Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
    Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
 */

package com.redeye.bastinger.Res;

/*import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;*/
import java.util.Random;

public class PieceGenerator {

	public static final int STRAT_RANDOM = 0;
	public static final int STRAT_7BAG = 1;
	public static final int STRAT_BASTET = 2;

	int strategy;
	int[] bag;
	int bagPointer;
	private final Random rndgen;
	
	public PieceGenerator(int strat) {
		bag = new int[7];
		for(int i = 0; i < 7; i++) //initial Permutation
			bag[i] = i;
		
		rndgen = new Random(System.currentTimeMillis());
		if(strat==STRAT_RANDOM)
			this.strategy = STRAT_RANDOM;
		else if (strat == STRAT_7BAG)
			this.strategy = STRAT_7BAG;
		else this.strategy = STRAT_BASTET;
		
		// Fill initial Bag
		for(int i = 0; i < 6; i++) {
			int c = rndgen.nextInt(7-i);
			int t = bag[i]; bag[i] = bag[i+c]; bag[i+c] = t;	/* swap */
		}
		bagPointer = 0;


	}

	public int next() {
		switch (strategy) {
			case STRAT_RANDOM:
				return rndgen.nextInt(7);
			case STRAT_7BAG:
				if (bagPointer < 7) {
					bagPointer++;
				} else {
					// Randomize Bag
					for (int i = 0; i < 6; i++) {
						int c = rndgen.nextInt(7 - i);
						int t = bag[i];
						bag[i] = bag[i + c];
						bag[i + c] = t;    /* swap */
					}
					bagPointer = 1;
				}
				return bag[bagPointer - 1];
			default:
				if (bagPointer < 7) {
					bagPointer++;
				} else {
					// Randomize Bag
					for (int i = 0; i < 6; i++) {
						int c = rndgen.nextInt(7 - i);
						int t = bag[i];
						bag[i] = bag[i + c];
						bag[i + c] = t;    /* swap */
					}
					bagPointer = 1;
				}
				return bag[bagPointer - 1];
		}
	}
}